//
//  CPresenceMgr.m
//  wanzhaoIM
//
//  Created by wanzhao on 15/12/3.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "CPresenceMgr.h"
#import "FMDB.h"
#import "CFolderMgr.h"
#import "MicroDef.h"
#import "CPresenceModel.h"

@implementation CPresenceMgr



-(BOOL)insetPresence:(CPresenceModel *)presence
{
    if(presence == nil)
    {
        return NO;
    } else {
        if (M_ISSTRNULL(presence.UserCode)) {
            return NO;
        }
    }
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"INSERT INTO PRESENCE(UserCode, IsAvailable, pcAvailable,yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (?, ?, ?,?,?,?)", M_STOREVALUE(presence.UserCode), M_STOREVALUE(presence.IsAvailable),M_STOREVALUE(presence.pcAvailable),M_STOREVALUE(presence.yuLiuZiduan1),@(presence.yuLiuZiduan2),M_STOREVALUE(presence.yuLiuZiduan3)];
    }];
    return ret;
}
//批量插入联系人
-(BOOL)batchInsertPresence:(NSArray *)presenceList
{
    
    if(presenceList == nil && [presenceList count] <= 0)
    {
        return NO;
    }
    //     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].companyShareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(CPresenceModel *presence in presenceList)
        {
            if (!M_ISSTRNULL(presence.UserCode)) {
               ret = [db executeUpdate:@"INSERT INTO PRESENCE(UserCode, IsAvailable, pcAvailable,yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (?, ?, ?,?,?,?)", M_STOREVALUE(presence.UserCode), M_STOREVALUE(presence.IsAvailable),M_STOREVALUE(presence.pcAvailable),M_STOREVALUE(presence.yuLiuZiduan1),@(presence.yuLiuZiduan2),M_STOREVALUE(presence.yuLiuZiduan3)];
                if(!ret)
                {
                    *rollback = YES;
                }
                
            }
        }
        
    }];
    return ret;
}

- (BOOL)updatePressenceWithPresence:(CPresenceModel *)presence
{
    if(presence == nil)
    {
        return NO;
    }
    //     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE PRESENCE SET IsAvailable = ?, pcAvailable = ? where UserCode = ?",M_STOREVALUE(presence.IsAvailable) ,M_STOREVALUE(presence.pcAvailable), M_STOREVALUE(presence.UserCode)];
    }];
    return ret;

}
- (CPresenceModel *)getPresenceWithUserCode:(NSString *)userCode
{
    if(M_ISSTRNULL(userCode))
    {
        return nil;
    }
    NSString *sql = nil;
    __block CPresenceModel *presence = nil;
    sql = [NSString stringWithFormat:@"select * from PRESENCE where  UserCode = \'%@\'", userCode];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            presence = [[CPresenceModel alloc] init];
            presence.presenIndex = [result  longLongIntForColumn:@"identifer"];
            presence.UserCode = [result stringForColumn:@"UserCode"];
            presence.IsAvailable = [result stringForColumn:@"IsAvailable"];
            presence.pcAvailable = [result stringForColumn:@"pcAvailable"];
        }
        //        CDepartmentModel *dep = [[[CDepartmentMgr alloc] init] loadDepartment:contact.DEPTID];
        //        contact.DEPNAME = dep.DepartName;
        [result close];
    }];
    return presence;

}

- (BOOL)updateAllPresenceIsUnAvailable
{
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE PRESENCE SET IsAvailable = ?, pcAvailable = ?;",@"离线",@"离线"];
        
    }];
    return ret;
    

}

@end
